package cn.clx.org.service.impl;

import cn.clx.basic.service.impl.BaseServiceImpl;
import cn.clx.org.domain.Deptpartment;
import cn.clx.org.mapper.DeptpartmentMapper;
import cn.clx.org.query.DeptpartmentQuery;
import cn.clx.org.service.IDeptpartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class DeptpartmentServiceImpl extends BaseServiceImpl<Deptpartment> implements IDeptpartmentService {

    @Autowired
    private DeptpartmentMapper deptpartmentMapper;

    /**
     * 新增
     * @param deptpartment
     */
    @Override
    public void save(Deptpartment deptpartment) {
        deptpartmentMapper.save(deptpartment);
        this.update(deptpartment);
    }

    /**
     * 修改
     * @param deptpartment
     */
    @Override
    public void update(Deptpartment deptpartment) {
        if(deptpartment.getParent_id()==null){
            String dirPathme="/"+deptpartment.getId();
            deptpartment.setDirPath(dirPathme);
        }else {
            //得到父亲的id
            Long parent_id = deptpartment.getParent_id();
            //根据父亲id查到父亲的dirPath
            String dirPathparent = deptpartmentMapper.loadById(parent_id).getDirPath();
            //而我的dirPath 是父亲的dirPath + / 自己id
            String dirPathme=dirPathparent+"/"+deptpartment.getId();
            deptpartment.setDirPath(dirPathme);
        }
        deptpartmentMapper.update(deptpartment);
    }

    /**
     * 批量删除
     * @param query
     */
    @Override
    public void deleteArray(DeptpartmentQuery query) {
        //先把字符串转换成字符串数组  split 以，来分割  "1,23,53,2,65"  ==> [1,23,53,2,65]
        String[] strArr = query.getIds().split(",");
        //定义一个一样长度的Long数组
        Long[] ids = new Long[strArr.length];
        //通过循环转变成Long元素，在进行赋值到Long数组中
        for(int a=0;a<strArr.length;a++){
            ids[a] = Long.valueOf(strArr[a]);
        }
        deptpartmentMapper.deleteArray(ids);
    }

    /**
     * 无线级树
     * @return
     */
    @Override
    public List<Deptpartment> getDeptTree() {
        //查出所有数据放到内存中
        List<Deptpartment> deptpartments = deptpartmentMapper.loadAll();
        //创建一个hashMap  把key作为id value 作为id所对应的对象
        HashMap<Long, Deptpartment> allDepts = new HashMap<>();
        for (Deptpartment deptpartment : deptpartments) {
            allDepts.put(deptpartment.getId(),deptpartment);
        }
        //准备一个集合来存数据
        List<Deptpartment> firstObject = new ArrayList<>();
        //把key作为id value 作为id所对应的对象
        for (Deptpartment temp : deptpartments) {
            if(temp.getParent_id()==null){
                //没有父id 说明是顶级
                firstObject.add(temp);
            }else {
                //不是顶级之后，得到它的父亲id 通过hashmap key(id) 得到父类对象
                Deptpartment parent = allDepts.get(temp.getParent_id());
                if(parent!=null){
                    //父类对象得到孩子对象，把刚刚那个有父类id的对象装进去
                    parent.getChildren().add(temp);
                }
            }
        }
        return firstObject;
    }

}
